import { storage, tokenTableName } from '@/config'

/**
 * 获取存储类型
 * @param {string} storage
 * @return {Storage}
 */
function getStorage(storage) {
  if ('localStorage' === storage) {
    return localStorage
  } else if ('sessionStorage' === storage) {
    return sessionStorage
  } else {
    return localStorage
  }
}

/**
 * @description 获取accessToken
 * @returns {string}
 */
export function getAccessToken() {
  let store = getStorage(storage)
  return store.getItem(tokenTableName)
}

/**
 * @description 存储accessToken
 * @param accessToken
 * @returns {void|*}
 */
export function setAccessToken(accessToken) {
  let store = getStorage(storage)
  return store.setItem(tokenTableName, accessToken)
}

/**
 * @description 移除accessToken
 * @returns {void|Promise<void>}
 */
export function removeAccessToken() {
  let store = getStorage(storage)
  return store.removeItem(tokenTableName)
}
